package ag.script.project.standard.entity;

import ag.script.project.standard.core.ScreensDes;
import ag.script.project.standard.ann.Ag;
import ag.script.project.standard.screen.ParserScreens;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AgAppConfig {

    @Ag("打包存放的名称")
    @SerializedName("name")
    private String name = "";

    @Ag("应用appId")
    @SerializedName("appId")
    private String appId = "";

    @Ag("应用appKey")
    @SerializedName("appKey")
    private String appKey = "";

    @Ag("开发者qq")
    @SerializedName("devQq")
    private String devQq = "";

    @Ag("开发者密码")
    @SerializedName("devPass")
    private String devPass = "";

    @Ag("[地址]应用描述")
    @SerializedName("descriptionPath")
    private String descriptionPath = "";

    @Ag("[地址]指定图标地址")
    @SerializedName("iconPath")
    private String iconPath = "";

    @Ag("[地址]资源列表")
    @SerializedName("resListPath")
    private String resListPath = "";

    @Ag("项目版本号")
    @SerializedName("version")
    private String version = "v0.0.1";

    @Ag("开发环境下的屏幕尺寸")
    @SerializedName("devScreen")
    private String devScreen = "1080x?";


    @SerializedName("screens")
    private List<String> screens = new ArrayList<>();

    public void addScreen(String screen) {
        if (StrUtil.isBlankIfStr(screen)) {
            return;
        }
        //如果书写格式是：1080*1920，替换成1080x1920
        String s_1 = StrUtil.replace(screen.trim().toLowerCase(), "*", "x");
        //如果书写格式是：1080x?，替换成1080x
        String s_2 = StrUtil.replace(s_1, "?", "");
        screens.add(s_2);
    }

    //获得开发环境屏幕描述信息
    public ScreensDes getDevScreenDes() {
        return new ParserScreens(this).parseSingle(this.devScreen);
    }

    //获得屏幕描述信息
    public List<ScreensDes> getScreenDes() {
        return new ParserScreens(this).parse();
    }

    public String getAppNamePinyin() {
        if (StrUtil.isBlankIfStr(this.name)) {
            return null;
        }
        String projectNamePinyin = PinyinUtil.getPinyin(this.name);
        List<String> split_py = StrUtil.split(projectNamePinyin, " ");
        StringBuilder sb = new StringBuilder();
        for (String py : split_py) {
            sb.append(StrUtil.upperFirst(py));
        }
        return sb.toString();
    }

    @Override
    public String toString() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        return gson.toJson(this);
    }
}
